﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Koonsoft.HYCRM.Tool
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始加密客户数据……");

            Console.WriteLine(EncryptHelper.Decrypt("T9SXfnhklEhulb+A+Y0eVgE45FZM/QoV", "3.141592"));

            //var sql = @"select * from client";

            //var sqlconn = new SqlConnection
            //                            {
            //                                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            //                            };
            //var sqlcmd = new SqlCommand
            //                 {
            //                     CommandText = sql,
            //                     CommandType = CommandType.Text,
            //                     Connection = sqlconn
            //                 };

            //try
            //{
            //    sqlcmd.Connection.Open();
            //    SqlDataReader sqlrd = sqlcmd.ExecuteReader();
            //    while (sqlrd.Read())
            //    {
            //        var clinetID = sqlrd["ClientID"].ToString();
            //        var address = sqlrd["Address"].ToString();
            //        var telphone = sqlrd["telephone"].ToString();
            //        var mobile = sqlrd["mobile"].ToString();

            //        UpdateAddress(mobile, telphone, address, clinetID);

            //        Console.WriteLine("加密客户 {0} 联系方式成功",sqlrd["ClientName"]);
            //    }
            //}
            //finally
            //{
            //    sqlcmd.Connection.Close();
            //}

            Console.WriteLine("加密数据完成……");

            Console.ReadLine();
        }

        public static void UpdateAddress(string mobile, string telphone, string address, string clientID)
        {
            var sql = @"update client set telephone=@telephone,mobile=@mobile,address=@address where cast(clientid as nvarchar(36))=@clientid";

            var sqlconn = new SqlConnection
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            };

            var sqlcmd = new SqlCommand
            {
                CommandText = sql,
                CommandType = CommandType.Text,
                Connection = sqlconn
            };

            if (!string.IsNullOrEmpty(telphone))
            {
                telphone = EncryptHelper.Encrypt(telphone, "3.141592");
            }
            sqlcmd.Parameters.Add("@telephone", SqlDbType.NVarChar, 64);
            sqlcmd.Parameters["@telephone"].Value = telphone;

            if (!string.IsNullOrEmpty(mobile))
            {
                mobile = EncryptHelper.Encrypt(mobile, "3.141592");
            }
            sqlcmd.Parameters.Add("@mobile", SqlDbType.NVarChar, 64);
            sqlcmd.Parameters["@mobile"].Value = mobile;

            if (!string.IsNullOrEmpty(address))
            {
                address = EncryptHelper.Encrypt(address, "3.141592");
            }
            sqlcmd.Parameters.Add("@address", SqlDbType.NVarChar, 512);
            sqlcmd.Parameters["@address"].Value = address;

            sqlcmd.Parameters.Add("@clientid", SqlDbType.NVarChar,36);
            sqlcmd.Parameters["@clientid"].Value = clientID;

            try
            {
                sqlcmd.Connection.Open();

                sqlcmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加密失败，原因{0}",ex.Message);
                return;
            }
            finally
            {
                sqlcmd.Connection.Close();
            }
        }


    }
}
